﻿using AutoMapper;
using B.S.Filtration.Domain;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.ProcessCommand;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Write.Api.Application.CommandHander.ProcessHandler
{
    public class UpdateProcessHandler : IRequestHandler<UpdateProcessCommand, APIResult<int>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public UpdateProcessHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<APIResult<int>> Handle(UpdateProcessCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var model = mapper.Map<Process>(request);
            model.IsDel = false;
            result.data = await db.Updateable(model).ExecuteCommandAsync();
            if (result.data > 0)
            {
                result.code = APIResultCode.success;
                result.msg = "工序列表修改成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "工序列表修改失败";
            }
            return result;
        }
    }
}
